//classe che estrae le chiavi RSA pubblica e privata dal certificato

package client;

import java.math.BigInteger;
import java.io.*;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

public class creaChiaviCifratura {

	public creaChiaviCifratura(){
		
	}

	
	public static void main(String[] args) {
	
		creaChiaviCifratura ccc = new creaChiaviCifratura();
		ccc.crea();
	}

	public void crea(){
		try {
			System.out.println("Inizio a generare le chiavi RSA... ");
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(1024);
			KeyPair kp = kpg.genKeyPair();
			KeyFactory fact =KeyFactory.getInstance("RSA");
			RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),RSAPublicKeySpec.class);
			RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),RSAPrivateKeySpec.class);
			
			System.out.println(pub);
			saveToFile("clientpublic.key", pub.getModulus(), pub.getPublicExponent());
			saveToFile("clientprivate.key", priv.getModulus(),priv.getPrivateExponent());
			//PublicKey pbk = kp.getPublic();
			//PrivateKey prk = kp.getPrivate();
			
			//System.out.println("Chiave Pubblica = "+ pbk);
			//System.out.println("Chiave Privata = "+ prk);
			
			
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlg");
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveToFile(String FileName, BigInteger mod, BigInteger exp){
		try {
			FileOutputStream f = new FileOutputStream (FileName);
			ObjectOutputStream o = new ObjectOutputStream(f);
			o.writeObject(mod);
			o.writeObject(exp);
			o.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public PrivateKey readClientPrivKeyFromFile (String fileName){
		PrivateKey privk=null;
		
			try {
			FileInputStream f = new FileInputStream(fileName);
			ObjectInputStream i = new ObjectInputStream(f);
			BigInteger mod = (BigInteger) i.readObject();
			BigInteger exp = (BigInteger) i.readObject();
			RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(mod, exp);
			KeyFactory fact = KeyFactory.getInstance("RSA");
			
			privk=fact.generatePrivate(keySpec);
			i.close();
			}
			
			
			
			
			
			
			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			finally {
				return privk;
			}
	}
	
	
	
	public PublicKey readClientPubKeyFromFile (String fileName){
		PublicKey pubk=null;
		
			try {
			FileInputStream f = new FileInputStream(fileName);
			ObjectInputStream i = new ObjectInputStream(f);
			BigInteger mod = (BigInteger) i.readObject();
			BigInteger exp = (BigInteger) i.readObject();
			RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod, exp);
			KeyFactory fact = KeyFactory.getInstance("RSA");
			
			//X509EncodedKeySpec 509spec = new X509EncodedKeySpec ();
			pubk=fact.generatePublic(keySpec);
			System.out.println(pubk);
			
			i.close();
			
				
			}
			
			
			
			
			
			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			finally {
				return pubk;
			}
	}
	
	
	
}
